package com.bookshop.interceptor.configuration;

import com.bookshop.configuration.AuthPathConfiguration;
import com.bookshop.interceptor.AuthInterceptor;
import com.bookshop.interceptor.CurrentLimiterInterceptor;
import com.bookshop.interceptor.PermissionInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {

    @Resource
    private AuthPathConfiguration authPathConfiguration;

    @Resource
    private AuthInterceptor authInterceptor;

    @Resource
    private PermissionInterceptor permissionInterceptor;

    @Resource
    private CurrentLimiterInterceptor currentLimiterInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // IP拦截器
        registry.addInterceptor(currentLimiterInterceptor)
                .addPathPatterns("/**").order(0);

        // 登录拦截器
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                // list.toArray(new T[0])是常用的写法
                .excludePathPatterns(authPathConfiguration.getPass().toArray(new String[0])).order(1);

        // 权限拦截器
        registry.addInterceptor(permissionInterceptor)
                .addPathPatterns("/**").order(2);

    }

}
